// ======================================================================
// Copyright (C) 2013 Hell-Prototypes. / www.hellprototypes.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// ======================================================================
#include <qglobal.h>
#include <qwt_painter.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_layout.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
#ifndef QWT_NO_OPENGL
#include <qevent.h>
#include <qwt_plot_glcanvas.h>
#endif
#include "plot_wave_form.h"

#ifndef QWT_NO_OPENGL
class GLCanvas: public QwtPlotGLCanvas
{
public:
    GLCanvas( QwtPlot *parent = NULL ):
        QwtPlotGLCanvas( parent )
    {
        setContentsMargins( 1, 1, 1, 1 );
    }

protected:
    virtual void paintEvent( QPaintEvent *event )
    {
        QPainter painter( this );
        painter.setClipRegion( event->region() );

        QwtPlot *plot = qobject_cast< QwtPlot *>( parent() );
        if ( plot )
            plot->drawCanvas( &painter );

        painter.setPen( palette().foreground().color() );
        painter.drawRect( rect().adjusted( 0, 0, -1, -1 ) );
    }
};
#endif

/*****************************************************************************/

plot_wave_form::plot_wave_form( QWidget *parent ): QwtPlot( parent )
{

    m_plot_raw_data = new plot_raw_data();

#if 0
    //Use GL to paint
    QwtPlotGLCanvas *plotCanvas = new GLCanvas();
    plotCanvas->setFrameStyle( QFrame::Box | QFrame::Plain );
    plotCanvas->setPalette( Qt::black );
    plotCanvas->setLineWidth(1);
    setCanvas( plotCanvas );
#else
    QwtPlotCanvas *canvas = new QwtPlotCanvas();
    canvas->setFrameStyle( QFrame::Box | QFrame::Plain );
    canvas->setLineWidth( 1 );
    canvas->setPalette( Qt::black );
    setCanvas( canvas );
#endif
    // Insert grid
    m_grid = new plot_grid();
    m_grid->attach( this );

    // Insert curve
    m_curve = new QwtPlotCurve( "Data Moving Right" );
    m_curve->setPen( Qt::green );
    m_curve->setData( m_plot_raw_data );//QwtSeriesData<int>
    m_curve->attach( this );

    //setAxisScale( QwtPlot::xBottom, 0, 10 );
    setAxisScale( QwtPlot::yLeft, -128, 128 );
    enableAxis(QwtPlot::xBottom, false);
    enableAxis(QwtPlot::yLeft, false);

    //this->startTimer(20);
    //m_sys_clock.start();
}

void plot_wave_form::start_plot()
{
    m_timer_id = startTimer(20);
}

void plot_wave_form::stop_plot()
{
    if(m_timer_id > 0) {
        killTimer(m_timer_id);
        m_timer_id = 0;
    }
}

bool plot_wave_form::new_data_arise(t_circular_buffer *cb)
{
    QVector<double> plot_raw_data;

    if(cb->data_length <= m_x_axis_scale) {
        int index = cb->start;
        for(int i=0; i<cb->data_length; i++) {
            plot_raw_data.prepend((double)cb->p_buffer[index] - 128);
            if(index == 0) {
                index = cb->size-1;
            } else {
                index--;
            }
        }
    } else {
        double step = cb->data_length/m_x_axis_scale;
        double index = cb->start;
        for(int i=0; i<m_x_axis_scale; i++) {
            plot_raw_data.prepend((double)cb->p_buffer[(int)index] - 128);
            if(index >= step) {
                index -= step;
            } else {
                index = cb->size - (step - index);
            }
        }
    }
    m_plot_raw_data->set_plot_raw_data(plot_raw_data);

    return true;
}

bool plot_wave_form::set_x_axis_sample_number(int x_axis_sample_number)
{
    m_x_axis_sample_number = x_axis_sample_number;

    if( rect().width() < m_x_axis_sample_number) {
        m_x_axis_scale = rect().width();
    } else {
        m_x_axis_scale = m_x_axis_sample_number;
    }
    setAxisScale( QwtPlot::xBottom, 0, m_x_axis_scale);

    m_plot_raw_data->set_x_axis_scale(m_x_axis_scale);

    return true;
}

void plot_wave_form::timerEvent( QTimerEvent *e )
{
    e = e;
    replot();
}

void plot_wave_form::resizeEvent(QResizeEvent * event)
{
    //qDebug(" w = %d, h= %d", event->size().width(), event->size().height());
    set_x_axis_sample_number(m_x_axis_sample_number);

    QwtPlot::resizeEvent(event);
}
